package com.slipper.system.dictionary.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.core.message.MessageUtil;
import com.slipper.core.message.Topic;
import com.slipper.system.dictionary.bo.DictionaryBO;
import com.slipper.system.dictionary.bo.DictionaryDataBO;
import com.slipper.system.dictionary.bo.DictionaryDataBOs;
import com.slipper.system.dictionary.dao.DictionaryDao;
import com.slipper.system.dictionary.dao.DictionaryDataDao;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@ReadTransactional
public class DictionaryDataService {
    @Resource
    private DictionaryDao dictionaryDao;
    @Resource
    private DictionaryDataDao dictionaryDataDao;
    
    public DictionaryDataBO queryBy(String id) {
        return dictionaryDataDao.queryBy(id);
    }

    public List<DictionaryDataBO> queryByDictId(String dictId) {
        DictionaryDataBO bo = new DictionaryDataBO();
        bo.setDictId(dictId);
        return dictionaryDataDao.queryList(bo);
    }

    public String queryDictKey(String dictId, String dictValue) {
        List<String> keyList = dictionaryDataDao.queryDictKey(dictId, dictValue);
        return keyList.isEmpty() ? null : keyList.getFirst();
    }

    public List<DictionaryDataBO> queryByDictName(String dictName) {
        return dictionaryDataDao.queryByDictName(dictName);
    }
    
    public PageInfo<DictionaryDataBO> queryPage(DictionaryDataBO bo) {
        if ("dictName".equals(bo.getSortField())) {
            bo.setSortField("d.name");
        }
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dictionaryDataDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(DictionaryDataBO bo){
        DictionaryBO dictionary = dictionaryDao.queryBy(bo.getDictId());
        bo.setDictName(dictionary.getName());
        int result;
        if (bo.getId() != null && !"".equals(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            result = this.dictionaryDataDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result = this.dictionaryDataDao.insert(bo);
        }
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }
    
    @WriteTransactional
    public int delete(DictionaryDataBO bo){
        int result = dictionaryDataDao.delete(bo);
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }

    public List<DictionaryDataBO> queryList(DictionaryDataBO bo) {
        return dictionaryDataDao.queryList(bo);
    }

    @WriteTransactional
    public int saves(DictionaryDataBOs bos) {
        DictionaryBO dictionary = dictionaryDao.queryBy(bos.getDictId());
        int result = 0;
        for (DictionaryDataBO bo : bos.getList()) {
            if (Tool.isBlank(bo.getId())) {
                bo.setId(UUID.getUUID());
                bo.setDictId(bos.getDictId());
                bo.setDictName(dictionary.getName());
                bo.setUpdateTime(System.currentTimeMillis());
                bo.setCreateTime(System.currentTimeMillis());
                result += dictionaryDataDao.insert(bo);
            } else {
                bo.setUpdateTime(System.currentTimeMillis());
                result += dictionaryDataDao.update(bo);
            }
        }
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }

    @WriteTransactional
    public void saveList(List<DictionaryDataBO> dataList) {
        if (dataList == null) return;

        dataList.forEach(data -> {
            if (dictionaryDataDao.queryBy(data.getId()) != null) {
                dictionaryDataDao.update(data);
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                data.setCreateTime(System.currentTimeMillis());
                dictionaryDataDao.insert(data);
            }
        });
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
    }

    public int saveByCode(DictionaryDataBOs bos) {
        if (bos.getDictId() == null || "".equals(bos.getDictId())) {
            return 0;
        }

        DictionaryBO dictionary = dictionaryDao.queryBy(bos.getDictId());
        DictionaryDataBO qo = new DictionaryDataBO();
        qo.setDictId(bos.getDictId());
        List<DictionaryDataBO> list = this.queryList(qo);
        
        int result = 0;
        for (DictionaryDataBO temp : bos.getList()) {
            for (DictionaryDataBO bo : list) {
                if (bo.getDictKey().equals(temp.getDictKey())) {
                    bo.setDictName(dictionary.getName());
                    bo.setDictValue(temp.getDictValue());
                    bo.setUpdateTime(System.currentTimeMillis());
                    result += this.dictionaryDataDao.update(bo);
                }
            }
        }
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }
}
